-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.py
More file actions
36 lines (29 loc) · 1.03 KB
/
Solution.py
File metadata and controls
36 lines (29 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def dfs(v, graph, visited, component):
visited[v] = True
component.append(v)
for neighbor in graph[v]:
if not visited[neighbor]:
dfs(neighbor, graph, visited, component)
def main():
V = int(input("Enter the number of vertices: "))
E = int(input("Enter the number of edges: "))
# Create the graph as an adjacency list
graph = [[] for _ in range(V)]
print("Enter the edges (u v) for each edge (0-indexed):")
for _ in range(E):
u, v = map(int, input().split())
# Since the graph is undirected, add both directions.
graph[u].append(v)
graph[v].append(u)
visited = [False] * V
components = []
for i in range(V):
if not visited[i]:
component = []
dfs(i, graph, visited, component)
components.append(component)
print("Connected Components:")
for idx, comp in enumerate(components, 1):
print(f"Component {idx}: {' '.join(map(str, comp))}")
if __name__ == "__main__":
main()